package ddl

import (
	"time"

	"code.byted.org/bytertc/whiteboard_server/biz/pkg/biz_error"
	"code.byted.org/bytertc/whiteboard_server/biz/service/object"
	"github.com/pkg/errors"
	"google.golang.org/protobuf/proto"
	"gorm.io/gorm"
)

const TableObjects = "objects"
const TablePackets = "packets"
const TableReplays = "replays"
const TableBoards = "boards"
const TablePages = "pages"
const TableConsoleConfigs = "console_configs"
const TableConsoleSwitchs = "console_switchs"
const TableTranscodeTask = "transcode_task"
const TableTranscodeResult = "transcode_result"
const TableRooms = "rooms"

type Objects struct {
	Id          int64  `gorm:"column:id" `
	AppId       string `gorm:"column:app_id"`
	RoomId      string `gorm:"column:room_id"`
	ObjectId    string `gorm:"column:object_id"`
	BoardId     int32  `gorm:"column:board_id"`
	PageId      string `gorm:"column:page_id"`
	ActionCount int32  `gorm:"column:action_count"`
	Initialized bool   `gorm:"column:initialized"`
	Version     int    `gorm:"column:version"`
	Data        []byte `gorm:"column:data"`
	gorm.DeletedAt
}

func NewObjects(element object.Element) (*Objects, error) {
	data, err := proto.Marshal(element.ToProto())
	if err != nil {
		return nil, biz_error.EncodeError.Wrap(errors.WithStack(err))
	}
	return &Objects{
		AppId:       element.GetAppId(),
		RoomId:      element.GetRoomId(),
		ObjectId:    element.GetObjId(),
		BoardId:     element.GetBoardId(),
		PageId:      element.GetPageId(),
		ActionCount: element.GetActionCount(),
		Version:     1,
		Data:        data,
		Initialized: element.GetInitStatus(),
	}, nil
}

type Boards struct {
	Id           int64     `gorm:"column:id" `
	AppId        string    `gorm:"column:app_id"`
	RoomId       string    `gorm:"column:room_id"`
	BoardId      int32     `gorm:"column:board_id"`
	Status       int       `gorm:"column:status"`
	CurrentPage  string    `gorm:"column:current_page"`
	CurrentStep  int32     `gorm:"column:current_step"`
	CreatedAt    time.Time `gorm:"column:created_at"`
	UpdatedAt    time.Time `gorm:"column:updated_at"`
	ControlCount int32     `gorm:"column:control_count"`
	Data         []byte    `gorm:"column:data"`
}

type BoardStatus int

const (
	Active BoardStatus = iota
	Inactive
)

type Pages struct {
	Id           int64  `gorm:"column:id" `
	AppId        string `gorm:"column:app_id"`
	RoomId       string `gorm:"column:room_id"`
	BoardId      int32  `gorm:"column:board_id"`
	PageId       string `gorm:"column:page_id"`
	PrePageId    string `gorm:"column:pre_page_id"`
	ControlCount int32  `gorm:"column:control_count"`
	Extra        string `gorm:"column:extra"`
	Data         []byte `gorm:"column:data"`
	DelTimeStamp int64  `gorm:"column:del_timestamp"`
	gorm.DeletedAt
}

type Packet struct {
	Id          int64  `gorm:"column:id" `
	AppId       string `gorm:"column:app_id"`
	RoomId      string `gorm:"column:room_id"`
	BoardId     int32  `gorm:"column:board_id"`
	ActionCount int32  `gorm:"column:action_count"`
	StartTime   uint64 `gorm:"column:start_time"`
	Version     int    `gorm:"column:version"`
	Data        []byte `gorm:"column:data"`
}

type Replay struct {
	Id          int64     `gorm:"column:id" `
	AccountId   string    `gorm:"column:account_id"`
	AppId       string    `gorm:"column:app_id"`
	RoomId      string    `gorm:"column:room_id"`
	BoardId     int32     `gorm:"column:board_id"`
	Bucket      string    `gorm:"column:bucket"`
	Region      string    `gorm:"column:region"`
	FileName    string    `gorm:"column:file_name"`
	StartTime   uint64    `gorm:"column:start_time"`
	EndTime     uint64    `gorm:"column:end_time"`
	MaxId       int64     `gorm:"column:max_id"` // packets 主键id
	ActionCount int32     `gorm:"column:action_count"`
	CreatedAt   time.Time `gorm:"column:created_at"`
}

type TranscodeTask struct {
	Id            int64   `gorm:"column:id"`
	AppId         string  `gorm:"column:app_id"`
	TaskId        string  `gorm:"column:task_id"`
	UserId        string  `gorm:"column:user_id"`
	TranscodeMode int     `gorm:"column:transcode_mode"`
	Priority      int     `gorm:"column:priority"`
	Status        int     `gorm:"column:status"`
	RetryTimes    int     `gorm:"column:retry_times"`
	Meta          string  `gorm:"column:meta"`
	Executor      string  `gorm:"column:executor"`
	FileID        string  `gorm:"column:file_id"`
	FileName      string  `gorm:"column:file_name"`
	FileSize      int     `gorm:"column:file_size"`
	TotalPages    int     `gorm:"column:total_pages"`
	CurPages      int     `gorm:"column:cur_pages"`
	Ratio         float64 `gorm:"column:ratio"`
	ErrCode       int     `gorm:"column:err_code"`
	ErrMsg        string  `gorm:"column:err_msg"`
}

type TranscodeResult struct {
	Id         int64  `gorm:"column:id"`
	AppId      string `gorm:"column:app_id"`
	TaskId     string `gorm:"column:task_id"`
	FileName   string `gorm:"column:file_name"`
	PageID     int    `gorm:"column:page_id"`
	ImgUrl     string `gorm:"column:img_url"`
	ThmUrl     string `gorm:"column:thm_url"`
	H5Url      string `gorm:"column:h5_url"`
	StorageKey string `gorm:"column:storage_key"`
}

type ConsoleConfig struct {
	Id             int64     `gorm:"column:id" `
	AppId          string    `gorm:"column:app_id"`
	ParentCategory int       `gorm:"column:parent_category"`
	Category       int       `gorm:"column:category"`
	SpaceType      int       `gorm:"column:space_type"`
	Space          string    `gorm:"column:space"`
	AccountId      string    `gorm:"column:account_id"`
	Region         string    `gorm:"column:region"`
	Deleted        int       `gorm:"column:deleted"`
	CreatedAt      time.Time `gorm:"column:created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at"`
}

type ConsoleSwitch struct {
	Id        int64     `gorm:"column:id" `
	AppId     string    `gorm:"column:app_id"`
	Category  int       `gorm:"column:category"`
	Status    int       `gorm:"column:status"`
	CreatedAt time.Time `gorm:"column:created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at"`
}

type Rooms struct {
	Id            int64     `gorm:"column:id" `
	AppId         string    `gorm:"column:app_id"`
	RoomId        string    `gorm:"column:room_id"`
	ActiveBoardId int32     `gorm:"column:active_board_id"`
	Status        int       `gorm:"column:status"`
	CreatedAt     time.Time `gorm:"column:created_at"`
	UpdatedAt     time.Time `gorm:"column:updated_at"`
	Data          []byte    `gorm:"column:data"`
}

type WsParam struct {
	AppID       string `json:"app_id"`
	RoomID      string `json:"room_id"`
	WsAppID     int32  `json:"ws_app_id"`
	WsProductID int32  `json:"ws_product_id"`
	WsService   int32  `json:"ws_service"`
	WsGroupID   string `json:"ws_group_id"`
}
